//
//  YTLeftModelCcontrolView.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/12.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTLeftModelCcontrolView.h"
#import "YTOneLevelCell.h"
#import "YTTwoLevelCell.h"

@interface YTLeftModelCcontrolView ()<UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
{
    BOOL orientation;//yes shu no heng
}
@property(nonatomic, strong)UIView *bgView;
@property(nonatomic, strong)UITableView *onelevelTableView;
@property(nonatomic, strong)UITableView *twolevelTableView;
@property(nonatomic, strong)NSMutableArray *oneDataSources;
@property(nonatomic, strong)NSMutableArray *twoDataSources;
@property(nonatomic, strong)LeftModelCcontrolViewBlock block;
@end

@implementation YTLeftModelCcontrolView
-(void)configDataSources
{
    [self.twoDataSources removeAllObjects];
    NSLog(@"%d", publicModel.photoModel);
    if (publicModel.photoModel == 0)
    {//拍照
        [_twoDataSources addObjectsFromArray:@[@[@"10", @"10a"], @[@"11", @"11a"], @[@"12", @"12a"], @[@"13", @"13a"], @[@"14", @"14a"]]];
    }else if (publicModel.photoModel == 1)
    {//长景拍
        [_twoDataSources addObjectsFromArray:@[@[@"22", @"22a"], @[@"23", @"23a"], @[@"24", @"24a"]]];
    }else if (publicModel.photoModel == 2)
    {//长曝光
        [_twoDataSources addObjectsFromArray:@[@[@"27", @"27a"], @[@"28", @"28a"]]];
    }
}
-(instancetype)initWithBlock:(LeftModelCcontrolViewBlock)block
{
    self = [super init];
    if (self)
    {
//        self.backgroundColor = [UIColor redColor];
//        self.frame = KeyWindow.bounds;
        self.block = block;
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        
        [self p_addSubViews];
        [self p_makeConstraints];
        self.userInteractionEnabled = YES;
        self.oneDataSources = [NSMutableArray array];
        self.twoDataSources = [NSMutableArray array];
        if (publicModel.isPhoto)
        {
            [self.oneDataSources addObjectsFromArray:@[@[@"06", @"06a", Language(@"单拍")], @[@"07", @"07a", Language(@"全景拍")], @[@"08", @"08a", Language(@"长曝光")]]] ;
            [self configDataSources];
            _onelevelTableView.hidden = NO;
            _twolevelTableView.hidden = NO;
        }else
        {
            [self.oneDataSources addObjectsFromArray:@[@[@"70", @"70a", Language(@"默认")], @[@"96", @"96a", Language(@"慢动作")], @[@"71", @"71a", Language(@"延时摄影")], @[@"72", @"72a", Language(@"移动延时摄影")]]] ;
            [_twoDataSources removeAllObjects];
            _onelevelTableView.hidden = NO;
            _twolevelTableView.hidden = YES;
        }
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self p_makeConstraints];
}
-(void)p_addSubViews
{
    [self addSubview:self.bgView];
    [_bgView addSubview:self.onelevelTableView];
    [_bgView addSubview:self.twolevelTableView];
}
-(void)p_makeConstraints
{
    if([UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortrait||[UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationPortraitUpsideDown)
    {//竖屏
        orientation = YES;
        [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(ScreenWidth);
            if (publicModel.isPhoto)
            {
                make.centerY.equalTo(self.mas_bottom).offset(-170);
                make.width.mas_equalTo(141);//加这个1是为了让frame有变化_leftModelCcontrolView里面的layoutSubviews才会走
            }else
            {
                make.centerY.equalTo(self.mas_bottom).offset(-135);
                make.width.mas_equalTo(71);//加这个1是为了让frame有变化_leftModelCcontrolView里面的layoutSubviews才会走
            }
            make.centerX.equalTo(self);
        }];
        _bgView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
        [_onelevelTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.equalTo(_bgView);
            make.width.mas_equalTo(70);
        }];
        [_twolevelTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.width.equalTo(_onelevelTableView);
            make.leading.equalTo(_onelevelTableView.mas_trailing);
        }];
    }else
    {//横屏
        orientation = NO;
        [_bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.leading.equalTo(self).offset(100);
            //                make.width.mas_equalTo(140);
            if (publicModel.isPhoto)
            {
                make.width.mas_equalTo(140);//加这个1是为了让frame有变化_leftModelCcontrolView里面的layoutSubviews才会走
            }else
            {
                make.width.mas_equalTo(70);//加这个1是为了让frame有变化_leftModelCcontrolView里面的layoutSubviews才会走
            }
        }];
        _bgView.transform = CGAffineTransformMakeRotation(0);
        [_onelevelTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.equalTo(_bgView);
            make.width.mas_equalTo(70);
        }];
        [_twolevelTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.width.equalTo(_onelevelTableView);
            make.leading.equalTo(_onelevelTableView.mas_trailing);
        }];
    }
    [_onelevelTableView reloadData];
    [_twolevelTableView reloadData];
}
-(UIView *)bgView
{
    if (!_bgView)
    {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = YTColorAlpha(70, 70, 70, 0.7);
        _bgView.userInteractionEnabled = YES;
    }
    return _bgView;
}
-(UITableView *)onelevelTableView
{
    if (!_onelevelTableView)
    {
        _onelevelTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _onelevelTableView.userInteractionEnabled = YES;
        _onelevelTableView.backgroundColor = [UIColor clearColor];
        _onelevelTableView.delegate = self;
        _onelevelTableView.dataSource = self;
        [_onelevelTableView registerClass:[YTOneLevelCell class] forCellReuseIdentifier:@"YTOneLevelCell"];
        _onelevelTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _onelevelTableView.tag = 701;
        _onelevelTableView.userInteractionEnabled = YES;
    }
    return _onelevelTableView;
}
-(UITableView *)twolevelTableView
{
    if (!_twolevelTableView)
    {
        _twolevelTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _twolevelTableView.userInteractionEnabled = YES;
        _twolevelTableView.backgroundColor = [UIColor clearColor];
        _twolevelTableView.delegate = self;
        _twolevelTableView.dataSource = self;
        [_twolevelTableView registerClass:[YTTwoLevelCell class] forCellReuseIdentifier:@"YTTwoLevelCell"];
        _twolevelTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _twolevelTableView.tag = 702;
        _twolevelTableView.userInteractionEnabled = YES;
    }
    return _twolevelTableView;
}
#pragma mark delegate datasources
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 701)
    {
        return self.oneDataSources.count;
    }else
    {
//        if (publicModel.photoModel == 0)
//        {
//            return 5;
//        }else if (publicModel.photoModel == 1)
//        {
//            return 3;
//        }else if (publicModel.photoModel == 2)
//        {
//            return 2;
//        }
        return self.twoDataSources.count;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 701)
    {
        return 75;
    }else
    {
        return 55;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 701)
    {
        YTOneLevelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTOneLevelCell" forIndexPath:indexPath];
        cell.indexPath = indexPath;
        [cell configData:_oneDataSources[indexPath.row]];
        if (orientation)
        {
            cell.transform = CGAffineTransformMakeRotation(M_PI / 2);
        }else
        {
            cell.transform = CGAffineTransformMakeRotation(0);
        }
        return cell;
    }else
    {
        YTTwoLevelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YTTwoLevelCell" forIndexPath:indexPath];
        cell.indexPath = indexPath;
        [cell configData:_twoDataSources[indexPath.row]];
        if (orientation)
        {
            cell.transform = CGAffineTransformMakeRotation(M_PI / 2);
        }else
        {
            cell.transform = CGAffineTransformMakeRotation(0);
        }
        return cell;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 701)
    {
        if (publicModel.isPhoto)
        {//拍照
            publicModel.photoModel = (int)indexPath.row;
            [self configDataSources];
            [_onelevelTableView reloadData];
            [_twolevelTableView reloadData];
        }else
        {//录像
            self.block();
            publicModel.videoModel = (int)indexPath.row;
            [_onelevelTableView reloadData];
        }
    }else if (tableView.tag == 702)
    {
        self.block();
        if (publicModel.photoModel == 0)
        {
            publicModel.modelSingle = (int)indexPath.row;
            switch (indexPath.row)
            {
                case 4:
                    //hdr
                    [deviceManager setHDR:!publicModel.isHDR];
                    break;
                default:
                    break;
            }
        }else if (publicModel.photoModel == 1)
        {
            publicModel.modelFull = (int)indexPath.row;
        }else if (publicModel.photoModel == 2)
        {
            publicModel.modelLong = (int)indexPath.row;
        }
        [_twolevelTableView reloadData];
    }
    [self tableView:tableView Select:indexPath];
}
-(void)tableView:(UITableView *)tableView Select:(NSIndexPath *)indexPath
{
    if (tableView.tag == 701)
    {
        if (publicModel.isPhoto)
        {//拍照
            switch (publicModel.photoModel)
            {
                case 0:
                {//单拍
                    NSLog(@"");
                }
                    break;
                case 1:
                {//全景拍
                    
                }
                    break;
                case 2:
                {//长曝光
                    
                }
                    break;
                    
                default:
                    break;
            }
        }else
        {//录像
            
            switch (publicModel.videoModel)
            {
                case 0:
                {//默认
                    publicModel.videoModel = 0;
                    [deviceManager configureCameraForHighestFrameRate];
                }
                    break;
                case 1:
                {//慢动作
                    publicModel.videoModel = 1;
                    [deviceManager configureCameraForHighestFrameRate];
                }
                    break;
                case 2:
                {//延时摄影
                    publicModel.videoModel = 2;
                    
                }
                    break;
                case 3:
                {//移动延时摄影
                    publicModel.videoModel = 3;
                }
                    break;
                default:
                    break;
            }
        }
        if (self.delegate && [self.delegate respondsToSelector:@selector(delegateLeftModelAction:)])
        {
            [self.delegate delegateLeftModelAction:indexPath];
        }
    }
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    self.block();
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"])
    {////判断如果点击的是tableView的cell，就把手势给关闭了
        return NO;
    }
    return YES;
}
@end
